﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Public Class ProviderManager
   Private Shared _assembly As Reflection.Assembly
   Private Shared _providerType As Type

   Public Delegate Function CreateProviderInstanceDelegate() As CMISObjectModel.IProvider

   Public Shared Property CreateProviderInstanceCallback() As CreateProviderInstanceDelegate

   Shared Sub New()
      CreateProviderInstanceCallback = New CreateProviderInstanceDelegate(AddressOf CreateProviderInstance)
   End Sub


   ''' <summary>
   ''' Creates an instance of the entry class of the provider.
   ''' </summary>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Private Shared Function CreateProviderInstance() As CMISObjectModel.IProvider
      If _assembly Is Nothing OrElse _providerType Is Nothing Then
         Dim dllFilepath As String = System.Configuration.ConfigurationManager.AppSettings("ProviderDLLPath")
         dllFilepath = dllFilepath.Replace("[$BaseDirectory]", System.AppDomain.CurrentDomain.BaseDirectory)

         Dim dllFileInfo As New IO.FileInfo(dllFilepath)
         'Check if dll exists
         If Not dllFileInfo.Exists Then
            Throw New IO.FileNotFoundException(String.Format(My.Resources.FileNotFound, dllFileInfo.FullName), dllFileInfo.FullName)
         End If

         Dim assembly As Reflection.Assembly = Nothing
         Try
            'Load assembly with reflection
            assembly = Reflection.Assembly.LoadFrom(dllFileInfo.FullName)
         Catch ex As IO.FileLoadException
            Throw New Exceptions.ProviderManagerFileLoadException(ex, dllFileInfo.FullName)
         End Try

         'Search the entry point(Class implementing IProvider)
         Dim providerType As Type = Nothing
         For Each type As Type In assembly.GetTypes
            If GetType(IProvider).IsAssignableFrom(type) Then
               providerType = type
               Exit For
            End If
         Next
         'Entry point not found
         If providerType Is Nothing Then
            Throw New Exceptions.ProviderEntryPointNotFoundException
         End If
         _assembly = assembly
         _providerType = providerType
      End If

      'Create instance
      Return CType(Activator.CreateInstance(_providerType), IProvider)
   End Function

   Private _provider As CMISObjectModel.IProvider
   Public Sub New()
      _provider = CreateProviderInstanceCallback.Invoke()
   End Sub
   ''' <summary>
   ''' Instance of the entry class of the provider.
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public ReadOnly Property Provider As CMISObjectModel.IProvider
      Get
         Return _provider
      End Get
   End Property
End Class
